﻿using System;

namespace Lesson10_封装_运算符重载
{
    #region 一 基本概念
    //概念
    //让自定义类与结构体
    //可以使用运算符

    //使用关键字
    //operator

    //特点
    //1.一定是一个公共的静态方法
    //2.返回值写在operator前
    //3.逻辑处理自定义

    //注意
    //1.条件运算符需要成对实现
    //2.一个符号可以多个重载
    //3.不能使用ref和out
    #endregion

    #region 二 基本语法
    //public static 返回类型 operator 运算符(参数列表)
    #endregion

    #region 三 实例
    class Position
    {
        public int x;
        public int y;
        public Position(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public Position()
        {
            ;
        }
        //+运算符重载
        public static Position operator +(Position a, Position b)
        {
            Position p = new Position();
            p.x = a.x + b.x;
            p.y = a.y + b.y;
            return p;
        }
        //再进行一次重载
        public static Position operator +(Position position , int value)
        {
            position.x += value;
            position.y += value;
            return position;
        }

        #region 五 可重载与不可重载的运算符

        #region 可重载的运算符
        //注意符号参数的数量
        //参数数量与符号不匹配会报错
        #region 算术运算符
        public static Position operator -(Position p1, Position p2)
        {
            Position p = new Position();
            p.x = p1.x - p2.x;
            p.y = p1.y - p2.y;
            return p;
        }
        public static Position operator *(Position p1, Position p2)
        {
            Position p = new Position();
            p.x = (p1.x * p2.x);
            p.y = (p1.y * p2.y);
            return p;
        }
        public static Position operator /(Position p1, Position p2)
        {
            Position p = new Position();
            p.x = (p1.x / p2.x);
            p.y = (p1.y / p2.y);
            return p;
        }
        public static Position operator %(Position p1, Position p2)
        {
            Position p = new Position();
            p.x = p1.x % p2.x;
            p.y = p1.y % p2.y;
            return p;
        }
        public static Position operator ++(Position p)
        {
            p.x++;
            p.y++;
            return p;
        }
        public static Position operator --(Position p)
        {
            p.x--;
            p.y--;
            return p;
        }

        #endregion

        #region 逻辑运算符
        public static bool operator !(Position p)
        {
            return false;
        }
        //逻辑与 逻辑或不支持重载
        #endregion

        #region 位运算符
        public static Position operator |(Position p1, Position p2)
        {
            return null;
        }
        public static Position operator &(Position p1, Position p2)
        {
            return null;
        }
        public static Position operator ^(Position p1, Position p2)
        {
            return null;
        }
        public static Position operator ~(Position p)
        {
            return null;
        }
        public static Position operator <<(Position p1, int value)
        {
            return null;
        }
        public static Position operator >>(Position p1, int value)
        {
            return null;
        }
        #endregion

        #region 条件运算符
        //条件运算符必须成对实现
        //例如 重载 < 同时也必须要重载 > ,重载 == 同时也要重载 != 不然会报错
        public static bool operator ==(Position p1, Position p2)
        {
            if(p1.x == p2.x && p1.y == p2.y)
            {
                return true;
            }
            return false;
        }
        public static bool operator !=(Position p1, Position p2)
        {
            if (p1.x != p2.x || p1.y != p2.y)
            {
                return true;
            }
            return false;
        }
        public static bool operator <(Position p1, Position p2)
        {
            return true;
        }
        public static bool operator >(Position p1, Position p2)
        {
            return true;
        }
        public static bool operator <=(Position p1, Position p2)
        {
            return true;
        }
        public static bool operator >=(Position p1, Position p2)
        {
            return true;
        }
        #endregion

        #endregion

        #region 不可重载的运算符
        //逻辑与 && 逻辑或 ||
        //索引符 []
        //强转运算符 ()
        //特殊运算符
        //点. 三目运算符 ? : 赋值符 =
        #endregion

        #endregion
    }

    #endregion


    class Program
    {
        static void Main(string[] args)
        {
            #region 四 使用
            Position p1 = new Position(2, 3);
            Position p2 = new Position(3, 4);
            Position p = p1 + p2;
            Console.WriteLine("x坐标{0}，y坐标{1}", p.x, p.y);
            //使用重载运算符时 要注意参数顺序
            //下面表达式写成5 + p会报错
            Position P = p + 5;
            Console.WriteLine("x坐标{0}，y坐标{1}", P.x, P.y);
            #endregion
        }
    }
}